package jwei.apps.db;

import java.util.ArrayList;
import java.util.List;

import jwei.apps.db.StyleCache.StylesCache;
import jwei.apps.objects.Style;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

public class StyleDb {

    private StyleDb() {
    }

    private static final StyleDb instance = new StyleDb();

    public static StyleDb getInstance() {
        return instance;
    }

    public void addStyle(ContentResolver contentResolver, Style s) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(StylesCache.STYLE_ID, s.getStyleId());
        contentValue.put(StylesCache.PRODUCT_ID, s.getProductId());
        contentValue.put(StylesCache.IMAGE_URL, s.getImageUrl());
        contentValue.put(StylesCache.PRODUCT_URL, s.getProductUrl());
        contentValue.put(StylesCache.PRODUCT_NAME, s.getProductName());
        contentValue.put(StylesCache.BRAND_NAME, s.getBrandName());
        contentValue.put(StylesCache.PRICE, s.getPrice());
        contentValue.put(StylesCache.IS_QUEUE, 0);
        contentResolver.insert(StylesCache.CONTENT_URI, contentValue);
    }

    public void addStyles(ContentResolver contentResolver, List<Style> styles) {
        List<ContentValues> contentValues = new ArrayList<ContentValues>();
        for (Style s : styles) {
            ContentValues contentValue = new ContentValues();
            contentValue.put(StylesCache.STYLE_ID, s.getStyleId());
            contentValue.put(StylesCache.PRODUCT_ID, s.getProductId());
            contentValue.put(StylesCache.IMAGE_URL, s.getImageUrl());
            contentValue.put(StylesCache.PRODUCT_URL, s.getProductUrl());
            contentValue.put(StylesCache.PRODUCT_NAME, s.getProductName());
            contentValue.put(StylesCache.BRAND_NAME, s.getBrandName());
            contentValue.put(StylesCache.PRICE, s.getPrice());
            contentValue.put(StylesCache.IS_QUEUE, 0);
            contentValues.add(contentValue);
        }
        contentResolver.bulkInsert(StylesCache.CONTENT_URI, contentValues.toArray(new ContentValues[0]));
    }

    public List<Style> getStylesByProductId(ContentResolver contentResolver, long productId) {
        List<Style> ret = new ArrayList<Style>();
        Cursor c = contentResolver.query(StylesCache.CONTENT_URI, null, StylesCache.PRODUCT_ID + "=" + productId, null,
                null);
        while (null != c && c.moveToNext()) {
            long styleId = c.getLong(c.getColumnIndex(StylesCache.STYLE_ID));
            Style s = new Style(styleId, productId);
            s.setProductUrl(c.getString(c.getColumnIndex(StylesCache.PRODUCT_URL)));
            s.setImageUrl(c.getString(c.getColumnIndex(StylesCache.IMAGE_URL)));
            s.setPrice(c.getString(c.getColumnIndex(StylesCache.PRICE)));
        }
        c.close();
        return ret;
    }

    public boolean isEmpty(ContentResolver contentResolver) {
        boolean ret = true;
        String[] projection = new String[] { StylesCache._ID };
        Cursor cursor = contentResolver.query(StylesCache.CONTENT_URI, projection, null, null, null);
        if (null != cursor && cursor.moveToNext()) {
            ret = false;
        }
        cursor.close();
        return ret;
    }

    public int getStyleQueueStatus(ContentResolver contentResolver, long productId) {
        int ret = 0;
        Cursor c = contentResolver.query(StylesCache.CONTENT_URI, new String[] { StylesCache.IS_QUEUE },
                StylesCache.PRODUCT_ID + "=" + productId, null, null);
        if (c.moveToFirst()) {
            ret = c.getInt(c.getColumnIndex(StylesCache.IS_QUEUE));
        }
        c.close();
        return ret;
    }

    public boolean updateStyleQueue(ContentResolver contentResolver, long productId, int status) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(StylesCache.IS_QUEUE, status);
        int rows = contentResolver.update(StylesCache.CONTENT_URI, contentValue, StylesCache.PRODUCT_ID + "="
                + productId, null);
        return (rows > 0);
    }

    public void refreshCache(ContentResolver contentResolver) {
        // delete all rows that aren't in queue
        contentResolver.delete(StylesCache.CONTENT_URI, StylesCache.IS_QUEUE + " = 0", null);
    }

}
